package com.ruoyi.admin.controller;

import com.ruoyi.app.domain.RelUserMember;
import com.ruoyi.app.domain.User;
import com.ruoyi.app.dto.UserDto;
import com.ruoyi.app.service.UserService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/*
@description:用户
@ClassName UserController
@author chen
@create 2025-03-27 21:39
@Version 1.0
*/
@Slf4j
@RestController("adminUserController")
@RequestMapping("user")
@AllArgsConstructor
public class UserController  extends BaseController
{

    final UserService userService;


    /*
     * @==============History===============<br/>
     * @Description //   获取用户列表
     * @Date 2025-3-27 21:37
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return com.ruoyi.framework.web.page.TableDataInfo
     */
    @GetMapping(value = "/getUserList",produces = "application/json;charset=utf-8")
    public TableDataInfo getUserList(@RequestParam Map<String, Object> params)
    {
        startPage();
        List<UserDto> userInfoList = userService.getUserInfoList(params);
        return getDataTable(userInfoList);
    }


    /*
     * @==============History===============<br/>
     * @Description //   用户更新
     * @Date 2025-3-27 21:42
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return com.ruoyi.framework.web.domain.AjaxResult
     */
    @PostMapping(value = "/updateUserInfo",produces = "application/json;charset=utf-8")
    public AjaxResult updateUserInfo(@RequestBody User user)
    {
        return userService.updateUserInfo(user);
    }


    /*
     * @==============History===============<br/>
     * @Description //   删除用户
     * @Date 2025-3-27 21:45
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return com.ruoyi.framework.web.domain.AjaxResult
     */
    @DeleteMapping(value = "/deleteUserInfo",produces = "application/json;charset=utf-8")
    public AjaxResult deleteUserInfo(@RequestParam("id") Long id)
    {
        return userService.deleteByPrimaryKey(id);
    }




    /*
     * @==============History===============<br/>
     * @Description //   下载导入模板
     * @Date 2025-3-29 20:17
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return void
     */
    @GetMapping("/downloadTemplate")
    public void downloadExcelTemplate(HttpServletResponse response)
    {
        try
        {
            // 1. 从resources读取文件流
            InputStream inputStream = Thread.currentThread().getContextClassLoader()
                    .getResourceAsStream("templates/template.xls");
            if (inputStream == null) {
                throw new FileNotFoundException("模板文件不存在");
            }

            // 2. 配置响应头
            response.setContentType("application/vnd.ms-excel");
            String encodedFileName = URLEncoder.encode("导出会员模板文件.xls", StandardCharsets.UTF_8.name());
            response.setHeader("Content-Disposition", "attachment; filename=\"" + encodedFileName + "\"");

            // 3. 流拷贝到响应输出流
            IOUtils.copy(inputStream, response.getOutputStream());
            response.flushBuffer();
        } catch (IOException e) {
            throw new RuntimeException("下载失败: " + e.getMessage());
        }
    }


    /*
     * @==============History===============<br/>
     * @Description //   导入用户数据
     * @Date 2025-3-29 20:37
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return org.springframework.http.ResponseEntity<java.lang.String>
     */
    @PostMapping("/uploadExcel")
    public ResponseEntity<String> uploadExcel(@RequestParam("file") MultipartFile file)
    {
        try
        {
            // 校验文件类型
            if (!Objects.requireNonNull(file.getContentType()).equals("application/vnd.ms-excel"))
            {
                return ResponseEntity.badRequest().body("仅支持.xls格式文件");
            }
            // 读取Excel内容
            return userService.uploadExcel(file);
        } catch (Exception e)
        {
            return ResponseEntity.internalServerError().body("文件处理失败：" + e.getMessage());
        }
    }



    @PostMapping("/payOrder")
    public AjaxResult payOrder(@RequestBody RelUserMember relUserMember)
    {
        return userService.payOrder(relUserMember);
    }

}
